﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Project.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project.WebAPI.Controllers
{
    /// <summary>
    /// 消息
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class MessageController : ControllerBase
    {
        /*
         * 即时消息可以考虑使用ImCore 来实现
         * 
         */




        private readonly IMessageService msgService;

        public MessageController(IMessageService msgService)
        {
            this.msgService = msgService;
        }

        /// <summary>
        /// 查询当前用户的所有消息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMessages()
        {
            var user = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Id").Value;

            var list = await msgService.GetList(m => m.To == Guid.Parse(user))
                .Select(m => new { m.Id, m.From, m.Content, m.CreateAt })
                .ToListAsync();
            return Ok(list);
        }

        /// <summary>
        /// 查询当前用户未读消息
        /// </summary>
        /// <returns></returns>
        [HttpGet("newMsg")]
        public async Task<IActionResult> GetNewMessage()
        {
            var user = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Id").Value;

            var list = await msgService.GetList(m => m.To == Guid.Parse(user) && m.State==0)
                .Select(m => new { m.Id, m.From, m.Content, m.CreateAt })
                .ToListAsync();
            return Ok(list);
        }

        /// <summary>
        /// 查询当前用户未读消息数量
        /// </summary>
        /// <returns></returns>
        [HttpGet("newMsgCount")]
        public async Task<IActionResult> GetNewMessageCount()
        {
            var user = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Id").Value;

            var count = await msgService.GetList(m => m.To == Guid.Parse(user) && m.State == 0)
                .CountAsync();
            return Ok(count);
        }

        /// <summary>
        /// 根据id删除消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            await msgService.DeleteAsync(id);
            return NoContent();
        }

        /// <summary>
        /// 根据id查看消息内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<IActionResult>  Get(Guid id)
        {
            var msg = await msgService.GetList(m => m.Id == id)
                .Select(m => new { m.Id, m.From, m.Content, m.CreateAt })
                .FirstOrDefaultAsync();
            if (msg!=null)
            {
                // 将消息标注为“已读”
                await msgService.EditAsync(new Models.Message() { Id = id, State = 1 }, "State");
                return Ok(msg);
            }
            return NotFound();
           
        }
    }
}
